require("../../$res/plugin/daterangepicker");
var moment = require("../../$res/plugin/daterangepicker/moment");
module.exports = {
    template: require("./tpl/timeRang.htm"),
    props: {
        value: Array
    },
    data: function () {
        return {
            selected: 1
        }
    },
    mounted: function () {
        var $this = this;
        var start = moment().weekday(1).endOf('day');
        var end = moment();
        var ele = $('.reportrange');

        function cb(start, end) {
            $('.rangeHoder').html(start.format('YYYY-MM-DD') + ' 至 ' + end.format('YYYY-MM-DD'));
        }

        ele.daterangepicker({
            startDate: start,
            endDate: end,
            opens: 'right'
        }, cb);
        cb(start, end);
        ele.on('apply.daterangepicker', function (ev, picker) {
            $this.selected = false;
            $this.$emit('input', [picker.startDate.format('YYYY-MM-DD'), picker.endDate.format('YYYY-MM-DD')]);
        });
    },
    destroyed: function () {
        $('.reportrange').data('daterangepicker').remove();
    },
    methods: {
        timeSelect: function (ret) {
            var $this = this;
            switch (ret) {
                case 1:
                    $this.selected = 1;
                    $this.$emit('input', [
                        moment().weekday(1).endOf('day').format('YYYY-MM-DD'),
                        moment().format('YYYY-MM-DD')]
                    );
                    break;
                case 2:
                    $this.selected = 2;
                    $this.$emit('input', [
                        moment().weekday(-6).startOf('day').format('YYYY-MM-DD'),
                        moment().weekday(0).endOf('day').format('YYYY-MM-DD')]
                    );
                    break;
                case 3:
                    $this.selected = 3;
                    $this.$emit('input', [
                        moment().startOf('month').format('YYYY-MM-DD'),
                        moment().endOf('month').format('YYYY-MM-DD')]
                    );
                    break;
                case 4:
                    $this.selected = 4;
                    $this.$emit('input', [
                        moment().subtract(1, 'month').startOf('month').format('YYYY-MM-DD'),
                        moment().subtract(1, 'month').endOf('month').format('YYYY-MM-DD')]
                    );
                    break;
            }
        }
    }
};
